package com.nuc.wxh.vaccine_appointment.vaccines.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.wxh.vaccine_appointment.base.exception.VaccineException;
import com.nuc.wxh.vaccine_appointment.base.result.R;
import com.nuc.wxh.vaccine_appointment.base.result.ResultCodeEnum;
import com.nuc.wxh.vaccine_appointment.base.util.JwtInfo;
import com.nuc.wxh.vaccine_appointment.base.util.JwtUtils;
import com.nuc.wxh.vaccine_appointment.base.util.MD5;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysUser;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysVaccinator;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.LoginVo;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.RegisterVo;
import com.nuc.wxh.vaccine_appointment.vaccines.service.SysUserService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前台系统用户信息 前端控制器
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Api(tags = "用户管理")
@CrossOrigin
@RestController
@RequestMapping("/user/sys-user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("注册用户")
    @PostMapping("register")
    public R save(@ApiParam(value = "用户对象", required = true) @RequestBody RegisterVo registerVo) {
        sysUserService.register(registerVo);
        return R.ok().message("注册成功");
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public R login(@ApiParam(value = "用户对象", required = true)@RequestBody LoginVo loginVo) {
        try {
            String token = sysUserService.login(loginVo);
            return R.ok().data("token", token);
        }catch (Exception e){
            return R.error().message("登录失败");
        }
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("get-login-info")
    public R getLoginInfo(HttpServletRequest request) {
        try {
            JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
            System.out.println(request);
            System.out.println(jwtInfo);
            return R.ok().data("userInfo", jwtInfo);
        } catch (Exception e) {
            throw new VaccineException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
    }

    @ApiOperation("根据id获取接种者信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "接种者ID", required = true) @PathVariable String id) {
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser != null) {
            return R.ok().data("item", sysUser);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("退出登录")
    @PostMapping("logout")
    public R logout() {
        return R.ok();
    }


    @ApiOperation("修改密码")
    @PostMapping("updatepwd")
    public R updatepwd(@ApiParam(value = "用户对象", required = true) @RequestBody RegisterVo registerVo) {
        try {
            sysUserService.updatepwd(registerVo);
            return R.ok().message("注册成功");
        }catch (Exception e){
            return  R.error().message("用户不存在");
        }

    }

}

